/**
 * 
 */
package org.mspring.mlog.service.impl;

import java.util.Date;
import java.util.List;

import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.mspring.mlog.entity.Attachment;
import org.mspring.mlog.entity.News;
import org.mspring.mlog.service.AttachmentService;
import org.mspring.mlog.service.NewsService;
import org.mspring.mlog.web.security.SecurityUtils;
import org.mspring.platform.persistence.hibernate.BaseHibernateDao;
import org.mspring.platform.persistence.hibernate.common.EnhancedRule;
import org.mspring.platform.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 
 * @author Gao Youbo
 * @since 2013年6月28日
 */
@Service
@Transactional
public class NewsServiceImpl extends BaseHibernateDao<News, Long> implements NewsService {

    @Autowired
    private AttachmentService attachmentService;

    @Override
    public News save(News object) {
        // TODO Auto-generated method stub
        if (object.getCreateTime() == null) {
            object.setCreateTime(new Date());
        }
        if (object.getAuthor() == null) {
            object.setAuthor(SecurityUtils.getCurrentUser());
        }
        if (StringUtils.isBlank(object.getStatus())) {
            object.setStatus(News.Status.DRAFT);
        }
        return super.save(object);
    }

    @Override
    public void update(News object) {
        // TODO Auto-generated method stub
        if (StringUtils.isBlank(object.getStatus())) {
            object.setStatus(News.Status.PUBLISH);
        }
        super.update(object);
    }

    @Override
    public void remove(Long id) {
        // TODO Auto-generated method stub
        if (id == null) {
            return;
        }
        List<Attachment> attachments = attachmentService.findAttachments(id.toString(), Attachment.AttachFromType.FROM_NEWS);
        for (Attachment attachment : attachments) {
            attachmentService.deleteAttachment(attachment.getId());
        }
        super.remove(id);
    }

    @Override
    public void remove(News object) {
        // TODO Auto-generated method stub
        if (object == null || object.getId() == null) {
            return;
        }
        remove(object.getId());
    }

    @Override
    public void remove(Long... ids) {
        // TODO Auto-generated method stub
        for (Long id : ids) {
            remove(id);
        }
    }

    @Override
    public List<News> top(int num) {
        // TODO Auto-generated method stub
        EnhancedRule rule = new EnhancedRule();
        rule.addOrder(Order.desc("createTime"));
        rule.add(Restrictions.eq("status", News.Status.PUBLISH));
        rule.setPageSize(num);
        return list(rule);
    }

    @Override
    public List<News> hot(int num, Date start, Date end) {
        // TODO Auto-generated method stub
        return null;
    }

}
